// 常用的工具函数称为hook函数
import { useIntersectionObserver,useIntervalFn } from '@vueuse/core'
import { ref } from 'vue'

// 组件数据懒加载 => 到了组件的可视区再发送组件对应的请求
export const useLazyData = (fn: () => void) => {
  const target = ref(null)
  const { stop } = useIntersectionObserver(
    target,
    ([{ isIntersecting }]) => {
      if (isIntersecting) {
        fn()
        stop()
      }
    },
    {
      // 第三个参数 => 临界值
      threshold: 0
    }
  )
  return target
}
export function useCountDown(startNum:number=60){
const time =  ref(0)
const{pause,resume } = useIntervalFn(()=>{
  time.value--
  if(time.value<=0) return pause()
},1000,{immediate:false})
const start = ()=>{
  time.value=startNum
  resume()
}
return{time,start}
}